package com.ruoyi.system.service;

import com.ruoyi.system.domain.Activities;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class LikeService {
    @Autowired
    private RedisTemplate redisTemplate;

    // 点赞，查看该用户userId是否在集合里，没在集合里，加进去，即点赞；在集合里，取消点赞，remove掉
    public void like(String userId, String entityLikeKey) {
        boolean isMember = redisTemplate.opsForSet().isMember(entityLikeKey, userId);
        if (isMember) {
            redisTemplate.opsForSet().remove(entityLikeKey, userId);// 取消点赞
        } else {
            redisTemplate.opsForSet().add(entityLikeKey, userId);
        }
    }

    // 查询某实体点赞的数量，看集合里有多少个userId
    public long findEntityLikeCount(String entityLikeKey) {
        return redisTemplate.opsForSet().size(entityLikeKey);
    }

    // 查询某人对某实体的点赞状态，看某userId是否在集合里，返回1点赞了，返回0没点赞（点踩可以返回-1）
    public int findEntityLikeStatus(String userId,String entityLikeKey) {
        return redisTemplate.opsForSet().isMember(entityLikeKey, userId) ? 1 : 0;
    }
}
